package com.ruoyi.core.util;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Random;

/**
 * @Description:
 * @Company: 北京卡拉卡尔股份技术有限公司
 * @Author: Darren Ho
 * @Date: 9/30/2020 09:49
 */
public final class ActivationCodeGeneratorUtil {

    private final static String SPLIT = ".";

    private ActivationCodeGeneratorUtil() {
    }

    public static String getVirtualCode(String code) {

        return new StringBuilder().append(Base64Util.encodeData(IdUtils.fastSimpleUUID())).append(SPLIT).append(Base64Util.encodeData(code)).append(SPLIT)
                                  .append(Base64Util.encodeData(IdUtils.fastSimpleUUID())).toString();
    }

    public static String getRealCode(String virtualCode) {
        Assert.hasText(virtualCode, "激活码不允许为空");
        String[] elements = StringUtils.delimitedListToStringArray(virtualCode, SPLIT);
        if (!virtualCode.contains(SPLIT) || elements.length != 3) {
            String message = "激活码格式无效异常";
            throw new CustomException(message);
        }
        return Base64Util.decodeData(elements[1]);
    }

    public static void main(String[] args) {

//        String virtualCode = getVirtualCode("123");
//        System.out.println(virtualCode);
//
//        System.out.println(getRealCode(virtualCode));

        String code = "cd5d9565fa3b4f70b0377e4c9912d0d4";
        char[] chars = code.toCharArray();

        System.out.println(chars.length);

        StringBuilder builder = new StringBuilder();

        for (int i = 1; i <= 8; i++) {

            int j = new Random().nextInt(chars.length);
            builder.append(chars[j]);

        }



        System.out.println(builder.toString());
        System.out.println(builder.toString().length());

    }
}
